#include "array.h"

// 打印数组
void print_array(const int *arr, int size) {
  printf("{ ");
  for (int i = 0; i < size; i++) {
    if (i == size - 1) printf("%d", *(arr+i));
    else printf("%d, ", *(arr + i));
  }
  printf(" }\n");
}

// 随机访问数组元素
int random_access(const int *arr, int size) {
  srand((unsigned)time(NULL)); 
  int random_index = rand() % size;
  return arr[random_index];
}

// 在原数组的基础上扩容数组
int *extend_array(int *arr, int size, int enlarge) {
  int i;
  int *res = (int *)malloc(sizeof(int) * (size + enlarge));
  for (i = 0; i < size; i++) res[i] = arr[i];
  for (i = size; i < size + enlarge; i++) res[i] = 0;
  print_array(res, size + enlarge);
  return res;
}

// 插入元素
void insert_elem(int *arr, int size, int num, int index) {
  for (int i = size - 1; i > index; i--) arr[i] = arr[i-1];
  arr[index] = num;
}

// 删除元素
void remove_elem(int *arr, int size, int index) {
  for (int i = index; i < size; i++) arr[i] = arr[i + 1];
}

// 查找元素
int find_elem(const int *arr, int size, int target) {
  for (int i = 0; i < size; i++) {
    if (arr[i] == target) return i;
  }
  return -1;
}

